package com.oddfar.campus.business.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.oddfar.campus.business.domain.Clqkxx;
import com.oddfar.campus.common.core.BaseMapperX;
import com.oddfar.campus.common.core.LambdaQueryWrapperX;
import com.oddfar.campus.common.domain.PageResult;
import org.apache.ibatis.annotations.*;
import org.springframework.transaction.annotation.Transactional;

public interface ClqkxxMapper extends BaseMapperX<Clqkxx> {

    default PageResult<Clqkxx> selectPage(Clqkxx clqkxx) {

        return selectPage(new LambdaQueryWrapperX<Clqkxx>()
                .eqIfPresent(Clqkxx::getUserId, clqkxx.getUserId())
                .eqIfPresent(Clqkxx::getPhonenumber, clqkxx.getPhonenumber())
                .eqIfPresent(Clqkxx::getClpzh, clqkxx.getClpzh())
                .eqIfPresent(Clqkxx::getCllxDm, clqkxx.getCllxDm())
                .orderByAsc(Clqkxx::getCreateTime)
        );

    }
    default PageResult<Clqkxx> selectPage_id(Clqkxx clqkxx) {

        return selectPage(new LambdaQueryWrapperX<Clqkxx>()
                .eqIfPresent(Clqkxx::getPhonenumber, clqkxx.getPhonenumber())
                .eqIfPresent(Clqkxx::getClpzh, clqkxx.getClpzh())
                .eqIfPresent(Clqkxx::getCllxDm, clqkxx.getCllxDm())
                .orderByAsc(Clqkxx::getCreateTime)
        );

    }

    default PageResult<Clqkxx> selectPage(Clqkxx clqkxx, Long userId) {

        return selectPage(new LambdaQueryWrapperX<Clqkxx>()
                .eq(Clqkxx::getUserId, userId)
                .eqIfPresent(Clqkxx::getPhonenumber, clqkxx.getPhonenumber())
                .eqIfPresent(Clqkxx::getClpzh, clqkxx.getClpzh())
                .eqIfPresent(Clqkxx::getCllxDm, clqkxx.getCllxDm())
                .orderByAsc(Clqkxx::getCreateTime)
        );

    }

    @Delete("DELETE FROM dj_clqkxx WHERE djxh in (#{djxh})")
    int deleteClqkxx(Long djxh);

    @Update("UPDATE dj_wzxx set clbz = '1' WHERE uuid in (#{uuid})")
    int deletewzxx(String uuid);
    @Update("UPDATE dj_wzxx set clsj = NOW() WHERE uuid in (#{uuid})")
    void setclsj(String uuid);

    @Select("SELECT user_id FROM dj_czxx WHERE phonenumber = #{phonenumber}")
    Long selectIDbyMobile(String phonenumber);


    /*
    用于检查该手机号对应的用户名下当前是否有车辆，如果没有则将此次添加的车辆设置为默认车辆
     */
    @Update("UPDATE dj_czxx \n" +
            "SET clpzh = #{clpzh}\n" +
            "WHERE phonenumber = #{phonenumber}\n" +
            "AND (SELECT COUNT(*) FROM dj_clqkxx WHERE phonenumber = #{phonenumber}) = 0")
    void checkIsFirstPlate(@Param("phonenumber") String phonenumber, @Param("clpzh") String clpzh);

}